using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace DBSourceToolsLib.UI.Forms
{
    public partial class ViewOptionsForm : Form
    {
        private string m_strDatabaseName;
        private string m_strViewOptionsXmlFile;
        private bool m_bHasReloaded = false;

        public ViewOptionsForm(string strDatabaseName, string strViewOptionsXmlFile)
        {
            InitializeComponent();

            this.m_strDatabaseName = strDatabaseName;
            this.m_strViewOptionsXmlFile = strViewOptionsXmlFile;

            if (System.IO.File.Exists(this.m_strViewOptionsXmlFile))
            {
                this.m_scriptOptionsDS.ReadXml(this.m_strViewOptionsXmlFile);
                this.m_scriptOptionsDS.ScriptOptions.AcceptChanges();
            }
            this.m_dbListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbListView.AddColumn("TableName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbListView.AddColumn("ScriptTable", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Checkbox);
            this.m_dbListView.Bind();
        }

        private void m_toolStripButtonReload_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            SourceDatabase oSourceDatabase
                = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(m_strDatabaseName);

            //DBSourceToolsLib.Commands.DatabaseCommand oDBLoadDb =
            //    new DBSourceToolsLib.Commands.DatabaseCommand(this,
            //        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadViewList);
            DBSourceToolsLib.ScriptOptionsLoader oLoader = new ScriptOptionsLoader(
                this, DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadViewList);

            oLoader.Execute(ref oSourceDatabase, null);

            this.m_scriptOptionsDS = oLoader.ProcScriptOptionsDS;
            this.m_dbListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbListView.Bind();
            //            this.m_scriptOptionsDS.ScriptOptions
            this.m_bHasReloaded = true;
            this.Cursor = Cursors.Default;

        }

        private void m_toolStripButtonSave_Click(object sender, EventArgs e)
        {
            this.m_scriptOptionsDS.AcceptChanges();
            FileUtils.EnsureFileIsWriteable(this.m_strViewOptionsXmlFile);
            this.m_scriptOptionsDS.WriteXml(this.m_strViewOptionsXmlFile);
        }

        private void ViewOptionsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_scriptOptionsDS.HasChanges())
            {
                if (MessageBox.Show(this, "Save Changes ?", "View Options", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FileUtils.EnsureFileIsWriteable(this.m_strViewOptionsXmlFile);
                    this.m_scriptOptionsDS.WriteXml(this.m_strViewOptionsXmlFile);
                }
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = true;
            }
            this.m_dbListView.Invalidate();

        }

        private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = false;
            }
            this.m_dbListView.Invalidate();

        }

        private void selectUsingFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SysObjects.UI.Forms.SelectFilterForm oSelectFilterForm = new DBSourceToolsLib.SysObjects.UI.Forms.SelectFilterForm();
            if (oSelectFilterForm.ShowDialog(this) == DialogResult.OK)
            {
                string strFilterString = oSelectFilterForm.SelectionFilter;
                if (!String.IsNullOrEmpty(strFilterString))
                {
                    foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
                    {
                        if (oRow["TableName"].ToString().IndexOf(strFilterString) == 0)
                        {
                            oRow["ScriptTable"] = true;
                        }
                    }
                }
            }
            this.m_dbListView.Invalidate();

        }

 
    }
}